//
//  StockInOutTableView.swift
//  TestAPIManager
//
//  Created by yq on 2020/10/21.
//  Copyright © 2020 杨庆. All rights reserved.
//

import UIKit
import RxSwift
import MJRefresh
import HandyJSON

enum StockInOutType {
    // 出库
    case stockIn
    // 入库
    case stockOut
}


class StockInOutTableView: UITableView {

    private let disBag = DisposeBag()
    var stockType: StockInOutType?
    weak var stockInOutVc:StockInOutController?
    private var page:Int = 0
    
    var datas:[Any] = [
        StockInOutModel(),
        StockInOutModel(),
        StockInOutModel(),
        StockInOutModel(),
        StockInOutModel()
    ]
    
    override func awakeFromNib() {
        super.awakeFromNib()
        
        /*基本配置*/
        let configure = {
            self.showsVerticalScrollIndicator = false
            self.showsHorizontalScrollIndicator = false
            self.regist(StockInOutCell.self)
            self.separatorStyle = .none
            self.dataSource = self
            self.delegate = self
        }
        
        /*刷新控件*/
        let setupRefreshView = {
            self.mj_header = MJRefreshNormalHeader(refreshingBlock: {
                self.page = 1
                self.requestData()
            })
            
            self.mj_footer = MJRefreshAutoNormalFooter(refreshingBlock: {
                self.page += 1
                self.requestData()
            })
        }
    
        configure()
        setupRefreshView()
    }
    
    
    func requestData() {
        
        switch stockType! {
        case .stockIn: // 入库单列表
            
            _ = kStockInOutApi.request(.appSearchWarehouseStockIn(pageNum: page), mapModel: BaseDataList<StockInOutModel>.self)
            .subscribe(onNext: { [weak self] result in
                self?.handleResult(result)
            })
        case .stockOut: // 出库单列表
            _ = kStockInOutApi.request(.appSearchWarehouseStockOut(pageNum: page), mapModel: BaseDataList<StockInOutModel>.self)
            .subscribe(onNext: { [weak self] result in
                self?.handleResult(result)
            })
        }
    }


    private func handleResult<T>(_ result: MoyaResult<BaseDataList<T>>) {
        self.mj_header?.endRefreshing()
        self.mj_footer?.endRefreshing()
        
        switch result {
        case .success(let md):
            if self.page == 1 { datas.removeAll() }
            datas.append(contentsOf: md.list)
            reloadData()
        case .failure(_, let msg):
            self.page -= 1
            print("\(msg)")
        }
    }
    
    
    
}


extension StockInOutTableView: UITableViewDelegate,UITableViewDataSource {

    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return datas.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(StockInOutCell.self , for: indexPath)
        
        let checkDetail = { [weak self] in
            let detailVc = StockInOutDetailController()
            self?.stockInOutVc?.navigationController?.pushViewController(detailVc, animated: true)
        }
        cell.checkDetailEvent = checkDetail
        return cell
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 201
    }
    
}
